package com.example.myapplication.tack_11;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.example.myapplication.R;
import com.example.myapplication.tack_11.adatper.ArrangeAdapter;
import com.example.myapplication.tack_11.data.ListData;
import com.example.myapplication.utils.OkHttpTool;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class HousingActivity extends AppCompatActivity {
    private ImageView exit;
    private List<String> listFenLei;
    private TabLayout fenlei;
    private SearchView shuosuo;
    private RecyclerView list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tack_11_activity_housing);
        init();
        exit_();
        getFenLei();
        getList("二手");
        shuosuo_();
    }

    private void shuosuo_() {
        shuosuo.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                updateSearchResults(query);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                search(newText);
                return true;
            }
        });
    }
    private void updateSearchResults(String query) {

       if(!"".equals(query) && !TextUtils.isEmpty(query)){
           ssList(query);
        }else{
           int selectedTabPosition = fenlei.getSelectedTabPosition();
           String s = fenlei.getTabAt(selectedTabPosition).getText().toString();
           getList(s);
       }
    }
    private void search(String query) {

      if(!"".equals(query) && !TextUtils.isEmpty(query)){
          ssList(query);
        }else if("".equals(query.trim())) {
          int selectedTabPosition = fenlei.getSelectedTabPosition();
          String s = fenlei.getTabAt(selectedTabPosition).getText().toString();
          getList(s);
      }


    }

    //搜索
    private void ssList(String query) {
        String url = "prod-api/api/house/housing/list?pageSize=10&pageNum=1";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, ListData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                ListData Data = (ListData)obj;
                List<ListData.RowsDTO> listData = new ArrayList<>();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        List<ListData.RowsDTO> listSS = new ArrayList<>();
                        for (ListData.RowsDTO listDatum : Data.getRows()) {
                            if(listDatum.getSourceName().toLowerCase().contains(query.toLowerCase())){
                                listData.add(listDatum);
                            }
                        }
                        ListAdapter(listData);
                    }
                });
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }


    private void getFenLei() {


        runOnUiThread(new Runnable() {
            @Override
            public void run() {

                for (String rowsDTO : listFenLei) {
                    TabLayout.Tab tab =  fenlei.newTab();
                    tab.setText(rowsDTO);
                    fenlei.addTab(tab);
                }
                fenleiAdapter();
            }

        });

    }

    private void fenleiAdapter() {
        fenlei.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                String houseType = listFenLei.get(position);
                getList(houseType);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void getList(String houseType) {
        String url = "prod-api/api/house/housing/list?houseType="+houseType+"&pageSize=10&pageNum=1";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, ListData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                ListData Data = (ListData)obj;
                List<ListData.RowsDTO> listData;
                listData = Data.getRows();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ListAdapter(listData);
                    }
                });
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void ListAdapter( List<ListData.RowsDTO> listData) {
        ArrangeAdapter arrangeAdapter = new ArrangeAdapter(listData, HousingActivity.this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(HousingActivity.this);
        list.setLayoutManager(linearLayoutManager);
        list.setAdapter(arrangeAdapter);
    }

    private void exit_() {
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HousingActivity.this.finish();
            }
        });
    }
    private void init() {
        exit = findViewById(R.id.exit);
        fenlei = findViewById(R.id.fenlei);
        list = findViewById(R.id.list);
        shuosuo = findViewById(R.id.shuosuo);

        listFenLei =new  ArrayList<>();
        listFenLei.add("二手");
        listFenLei.add("租房");
        listFenLei.add("楼盘");
        listFenLei.add("中介");
    }
}